package com.ruoyi.wisdom.event;

import com.ruoyi.wisdom.domain.IotStudentParent;
import com.ruoyi.wisdom.domain.swap.SwapStudentParent;
import com.ruoyi.wisdom.service.IIotStudentParentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class StudentParentEvent extends IotEvent {

    @Autowired
    private IIotStudentParentService iotStudentParentService;

    /**
     * 处理
     *
     * @param swapStudentParent
     */
    public void handle(SwapStudentParent swapStudentParent) {
        if (swapStudentParent.getOperate() == null) {
            save(swapStudentParent);
        } else {
            switch (swapStudentParent.getOperate()) {
                case "delete":
                case "remove":
                    delete(swapStudentParent);
                    break;
                default:
                    save(swapStudentParent);
            }
        }
    }

    /**
     * 添加|编辑
     *
     * @param swapStudentParent
     */
    public void save(SwapStudentParent swapStudentParent) {
        IotStudentParent result = iotStudentParentService.selectIotStudentParentById(swapStudentParent.getId());
        // 实体装换
        IotStudentParent iotStudentParent = getModelMapper().map(swapStudentParent, IotStudentParent.class);
        if (result != null) {
            iotStudentParent.setId(result.getId());
            iotStudentParentService.updateIotStudentParent(iotStudentParent);
        } else {
            iotStudentParentService.insertIotStudentParent(iotStudentParent);
        }
    }

    /**
     * 删除
     *
     * @param swapStudentParent
     */
    public void delete(SwapStudentParent swapStudentParent) {
        iotStudentParentService.deleteIotStudentParentById(swapStudentParent.getId());
    }
}
